package com.summer.bilibili.socket;

import com.corundumstudio.socketio.SocketIONamespace;
import com.corundumstudio.socketio.SocketIOServer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 对所有注册的组件进行处理
 */
@Component
public class SocketListenerRegister implements BeanPostProcessor {
    @Resource
    private SocketIOServer socketIOServer;

    @Override
    public Object postProcessAfterInitialization ( Object bean , String beanName ) throws BeansException {
        if ( bean instanceof SocketListener ) {
            SocketListener socketListener = (SocketListener) bean; // 扫描 Spring 容器中所有实现了 SocketListener的 Bean。
            SocketIONamespace socketIONamespace = socketIOServer.addNamespace ( socketListener.getNamespace ( ) );// 为每个 Bean 创建一个 SocketIONamespace。
            socketIONamespace.addConnectListener ( socketListener ); // 添加连接监听器
            socketIONamespace.addDisconnectListener ( socketListener ); // 添加断开监听器
            socketIONamespace.addAuthTokenListener ( socketListener ); // 添加认证令牌监听器
        }
        return bean;
    }
}
